import Base from './Base.js';
import { GRAPH_TYPES } from './Graph.enum.js';
import Color from '../Color';

class Circle extends Base {
    constructor() {
        super(...arguments);
        this.name = GRAPH_TYPES.CIRCLE;
    }

    initUpdate(start, end) {
        this.points[0] = end;
        this.radius = Math.round(Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)));
    }
    update(i, pos) {
        if (i == Infinity) {
            var x1 = pos.x - this.x,
                y1 = pos.y - this.y;
            this.points[0].x += x1;
            this.points[0].y += y1;
            this.x = pos.x;
            this.y = pos.y;
        } else {
            this.points[0] = pos;
            this.radius = Math.round(Math.sqrt(Math.pow(pos.x - this.x, 2) + Math.pow(pos.y - this.y, 2)));
        }
    }

    // overwrite
    getDimensions() {
        let minx = this.x - this.radius,
            maxx = this.x + this.radius,
            miny = this.y - this.radius,
            maxy = this.y + this.radius,
            ratio = 1;
        return [minx * ratio, maxx * ratio, miny * ratio, maxy * ratio];
    }

    createPath() {
        this.ctx.beginPath();
        this.ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    }
    drawPath(ctx) {
        ctx.beginPath();
        let y = this.isReverse ? -this.y : this.y;
        ctx.arc(this.x, y, this.radius, 0, Math.PI * 2, false);
        ctx.closePath();
    }
    // 绘制两点之间线长度（同卷尺功能）
    drawLineLength(scaling) {
        if (!this.points || !this.points.length) return;

        let textSize = this.lineWidth;
        if (scaling) {
            textSize = (1 / scaling) * 20;
        }
        // console.log('Circle.drawLineLength: ', ctx);
        let lineLength = this.getLineLength(this.points[0], { x: this.x, y: this.y }),
            middlePoint = this.getLineMiddlePoint(this.points[0], { x: this.x, y: this.y });
        // textFontSize = this.lineWidth * 30;
        // 绘制圆半径
        this.drawCircleRadius();
        // this.ctx.beginPath();
        // this.ctx.moveTo(this.x, this.y);
        // this.ctx.lineTo(this.points[0].x, this.points[0].y);
        // this.ctx.stroke();
        // this.ctx.restore();

        // 绘制半径长
        this.ctx.save();
        this.ctx.font = textSize + 'px Arial';
        this.ctx.fillStyle = 'rgba(0, 0, 0, 0.8)';
        this.ctx.textAlign = 'center';
        this.ctx.textBaseline = 'bottom';
        this.ctx.fillText(lineLength, middlePoint.x, -middlePoint.y);
        this.ctx.stroke();
        this.ctx.restore();
    }

    // 绘制圆半径
    drawCircleRadius() {
        // console.log('Circle.drawCircleRadius: ');
        let penSize = (1 / this.scaling) * 2 * this.ratio;
        let yr = this.isReverse ? -1 : 1;
        this.ctx.save();
        this.ctx.beginPath();
        this.ctx.lineWidth = penSize / 2;
        this.ctx.strokeStyle = '#333333';
        this.ctx.setLineDash([penSize, penSize]);
        this.ctx.moveTo(this.x, this.y * yr);
        this.ctx.lineTo(this.points[0].x, this.points[0].y * yr);
        this.ctx.stroke();
        this.ctx.restore();
    }

    drawSelected(ctx) {
        if (ctx) this.ctx = ctx;
        this.ctx.save();
        this.ctx.lineWidth = this.lineWidth;
        this.ctx.strokeStyle = this.strokeStyle;
        this.drawPath(this.ctx);
        this.ctx.stroke();
        this.ctx.restore();
    }

    draw(ctx, drawDashLine = false) {
        if (ctx) this.ctx = ctx;
        this.ctx.save();
        this.drawPath(this.ctx);
        if (this.fillStyle) {
            this.ctx.fillStyle = this.fillStyle;
            this.ctx.fill();
        }
        if (drawDashLine) {
            let pen = 1 / this.scaling || 100;
            this.ctx.lineWidth = pen * 10;
            this.ctx.strokeStyle = Color.lightenDarkenColor(this.fillStyle, 0.3);
            this.ctx.setLineDash([pen * 20, pen * 20]);
            this.ctx.lineCap = 'round';
        } else {
            this.ctx.lineWidth = this.lineWidth;
            this.ctx.strokeStyle = this.strokeStyle;
        }
        this.ctx.stroke();
        this.ctx.restore();

        //绘制平移/更新图形锚点基准点
        // this.drawAnchorPoint();
        //绘制未连接点 - 未避免车辆绘制，修改到Painter中调用
        // this.drawUnConnectPoints();
    }

    drawCached(ctx) {
        ctx.save();
        // this.drawPath(ctx);
        let penSize = 1 / this.scaling || 100;
        // ctx.fillStyle = Color.lightenDarkenColor(this.fillStyle, 0.3);
        // ctx.fill();
        ctx.lineWidth = penSize * 5;
        ctx.strokeStyle = 'rgba(0, 0, 255, 0.6)';
        ctx.setLineDash([penSize * 10, penSize * 10]);
        ctx.lineCap = 'round';
        ctx.stroke();
        ctx.restore();
    }
    getSVGCodes() {
        // <circle stroke="rgba(0,0,0,0.00)" fill="rgba(0,0,0,0.00)" cx="500" cy="0" r="1414.21"></circle>
        let codes = '<circle ';
        codes += 'stroke="' + this.strokeStyle + '" ';
        codes += 'stroke-width="' + this.lineWidth + '" ';
        codes += 'fill="' + this.fillStyle + '" ';
        codes += 'cx="' + this.x + '" ';
        codes += 'cy="' + this.y + '" ';
        codes += 'r="' + this.radius + '" ';
        codes += '></circle>';
        return codes;
    }
}
export default Circle;
